package com.liuguang.framework.utils;

import android.util.Base64;
import com.bumptech.glide.load.Key;
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Base64Util {
    private static final String CipherMode = "AES/CBC/PKCS5Padding";
    public static final String IV = "w2wJCnctEG1sdfsW";
    public static final String KEY = "aScGU3fj8mcwvjsdsWWEE=";

    private static SecretKeySpec createKey(String str) {
        byte[] bArr;
        if (str == null) {
            str = "";
        }
        StringBuffer stringBuffer = new StringBuffer(32);
        stringBuffer.append(str);
        while (stringBuffer.length() < 16) {
            stringBuffer.append("0");
        }
        if (stringBuffer.length() > 16) {
            stringBuffer.setLength(32);
        }
        try {
            bArr = stringBuffer.toString().getBytes(Key.STRING_CHARSET_NAME);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            bArr = null;
        }
        return new SecretKeySpec(bArr, "AES");
    }

    private static IvParameterSpec createIV(String str) {
        byte[] bArr;
        if (str == null) {
            str = "";
        }
        StringBuffer stringBuffer = new StringBuffer(16);
        stringBuffer.append(str);
        while (stringBuffer.length() < 16) {
            stringBuffer.append("0");
        }
        if (stringBuffer.length() > 16) {
            stringBuffer.setLength(16);
        }
        try {
            bArr = stringBuffer.toString().getBytes(Key.STRING_CHARSET_NAME);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            bArr = null;
        }
        return new IvParameterSpec(bArr);
    }

    private static byte[] encrypt(byte[] bArr, String str, String str2) {
        try {
            SecretKeySpec createKey = createKey(str);
            Cipher instance = Cipher.getInstance(CipherMode);
            instance.init(1, createKey, createIV(str2));
            return instance.doFinal(bArr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encrypt(String str, String str2, String str3) {
        byte[] bArr;
        try {
            bArr = str.getBytes(Key.STRING_CHARSET_NAME);
        } catch (Exception e) {
            e.printStackTrace();
            bArr = null;
        }
        return Base64.encodeToString(encrypt(bArr, str2, str3), 0);
    }

    private static byte[] decrypt(byte[] bArr, String str, String str2) {
        try {
            SecretKeySpec createKey = createKey(str);
            Cipher instance = Cipher.getInstance(CipherMode);
            instance.init(2, createKey, createIV(str2));
            return instance.doFinal(bArr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decrypt(String str, String str2, String str3) {
        byte[] bArr;
        String str4;
        try {
            bArr = Base64.decode(str, 0);
        } catch (Exception e) {
            e.printStackTrace();
            bArr = null;
        }
        byte[] decrypt = decrypt(bArr, str2, str3);
        if (decrypt == null) {
            return null;
        }
        try {
            str4 = new String(decrypt, Key.STRING_CHARSET_NAME);
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
            str4 = null;
        }
        return str4;
    }
}
